package com.ruoyi.tb.lmt.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tb.lmt.domain.bo.TbParkingBo;
import com.ruoyi.tb.lmt.domain.vo.TbParkingVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.TbCarparkingVo;
import com.ruoyi.tb.lmt.domain.bo.TbCarparkingBo;
import com.ruoyi.tb.lmt.service.ITbCarparkingService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 存车场信息
 *
 * @author ruoyi
 * @date 2024-05-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/carparking")
public class TbCarparkingController extends BaseController {

    private final ITbCarparkingService iTbCarparkingService;

    /**
     * 查询存车场信息列表
     */
    //@SaCheckPermission("xha-lmt:carparking:list")
    @GetMapping("/list")
    public TableDataInfo<TbCarparkingVo> list(TbCarparkingBo bo, PageQuery pageQuery) {
        return iTbCarparkingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出存车场信息列表
     */
    //@SaCheckPermission("xha-lmt:carparking:export")
    @Log(title = "存车场信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbCarparkingBo bo, HttpServletResponse response) {
        List<TbCarparkingVo> list = iTbCarparkingService.queryList(bo);
        ExcelUtil.exportExcel(list, "存车场信息", TbCarparkingVo.class, response);
    }

    /**
     * 获取存车场信息详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("xha-lmt:carparking:query")
    @GetMapping("/{id}")
    public R<TbCarparkingVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTbCarparkingService.queryById(id));
    }

    /**
     * 新增存车场信息
     */
    //@SaCheckPermission("xha-lmt:carparking:add")
    @Log(title = "存车场信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TbCarparkingBo bo) {
        return toAjax(iTbCarparkingService.insertByBo(bo));
    }

    /**
     * 修改存车场信息
     */
    //@SaCheckPermission("xha-lmt:carparking:edit")
    @Log(title = "存车场信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbCarparkingBo bo) {
        return toAjax(iTbCarparkingService.updateByBo(bo));
    }

    /**
     * 删除存车场信息
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("xha-lmt:carparking:remove")
    @Log(title = "存车场信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTbCarparkingService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 修改存车场信息
     */
    @Log(title = "修改存车场信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateCarParking")
    public R<Void> updateCarParking(@Validated(EditGroup.class) @RequestBody TbCarparkingBo bo) {
        TbCarparkingVo tbCarparkingVo = iTbCarparkingService.queryById(bo.getId());
        if (tbCarparkingVo!=null){
            if (!bo.getLmtNumber().equals(tbCarparkingVo.getLmtNumber())&& StringUtils.isNotBlank(bo.getLmtNumber())){
                TbCarparkingBo bo1 = new TbCarparkingBo();
                bo1.setLmtNumber(bo.getLmtNumber());
                List<TbCarparkingVo> tbParkingVos = iTbCarparkingService.queryList(bo1);
                if (tbParkingVos.size()>0){
                    TbCarparkingVo tbParkingVo1 = tbParkingVos.get(0);
                    TbCarparkingBo bo2 = new TbCarparkingBo();
                    bo2.setId(tbParkingVo1.getId());
                    bo2.setLmtNumber("");
                    bo2.setLmtType("");
                    bo2.setParkingNeed("");
                    TbCarparkingBo bo3 = iTbCarparkingService.updateCarParking(bo2);
                    iTbCarparkingService.updateByBo(bo3);
                }
            }
        }
        TbCarparkingBo bos = iTbCarparkingService.updateCarParking(bo);
        return toAjax(iTbCarparkingService.updateByBo(bos));
    }
}
